from google.appengine.api import users
from task import articletask
from task import documenttask
from task import userprofiletask
from task import discussiontask
from config import sitesettings

class BasePresenter(object):

    def __init__(self, currentuser, view, request):
        self.settings = sitesettings.SiteSettings()
        self.template_values={}
        self.doctask = documenttask.DocumentTask()
        self.articletask = articletask.ArticleTask()
        self.userprofiletask = userprofiletask.UserProfileTask()
        self.discussiontask = discussiontask.DiscussionTask()
        self.setNavDocCategories()
        self.setNavCategories()
        self.setNavDiscussionTypes()
        self.view = view
        self.request = request
        self.currentuser = currentuser
        self.currentuserprofile = None
        self.generateLoginUrl()
        self.template_values['currenttopics'] = self.discussiontask.getCurrentTopics(3)
        self.template_values['newsletterurl'] = self.settings.newsletterurl
        self.sitetitle = self.settings.sitetitle
        self.sitetitleshort = self.settings.sitetitleshort
        self.template_values['sitetitle'] = self.sitetitle
        self.template_values['sitetitleshort'] = self.sitetitleshort
        if (self.settings.showdocuments):
            self.template_values['showdocuments'] = self.settings.showdocuments
        if (self.settings.showdiscussions):
            self.template_values['showdiscussions'] = self.settings.showdiscussions
        if (self.settings.showmembership):
            self.template_values['showmembership'] = self.settings.showmembership
        if (self.settings.shownewsletter):
            self.template_values['shownewsletter'] = self.settings.shownewsletter
        if (self.settings.showmainfeature):
            self.template_values['showmainfeature'] = self.settings.showmainfeature

    def generateLoginUrl(self):
        if self.currentuser:
            username=self.currentuser.nickname()
            logouturl=users.create_logout_url(self.request.uri)
            self.template_values['username'] = username
            self.template_values['logouturl'] = logouturl
            self.template_values['is_admin'] = users.IsCurrentUserAdmin()
            self.currentuserprofile = self.userprofiletask.getByUser(self.currentuser)
            if self.currentuserprofile:
                self.template_values['nickname'] = self.currentuserprofile.nickname
                self.template_values['datejoined'] = self.currentuserprofile.datejoined
                self.template_values['commentcount'] = self.currentuserprofile.commentcounter
                self.template_values['lastpost'] = self.currentuserprofile.lastpost
                self.template_values['subscriber'] = self.currentuserprofile.subscriber
            else:
                if self.userprofiletask.hasProfile(self.currentuser):
                    self.template_values['profiledisabled'] = True
        else:
            loginurl = users.create_login_url(self.request.uri)
            self.template_values['loginurl'] = loginurl

    def setCurrentUser(self, currentuser):
        self.currentuser = currentuser
        self.generateLoginUrl()

    def setView(self, view):
        self.view = view

    def setRequest(self, request):
        self.request = request

    def setNavDocCategories(self):
        navdoccategories = self.settings.documentcategories
        self.template_values['navdoccategories'] = navdoccategories

    def setNavCategories(self):
        navcategories = self.settings.articlecategories
        self.template_values['navcategories'] = navcategories

    def setNavDiscussionTypes(self):
        self.navdiscussiontypes = self.settings.discussiontypes
        self.template_values['navdiscussiontypes'] = self.navdiscussiontypes

    def respondToGet(self):
        page_title = self.settings.sitetitle
        self.template_values['page_title'] = page_title
        self.view.showGetResponse(template_values)
